class EditionDate < ActiveRecord::Base
  belongs_to :full_date
  belongs_to :edition
  belongs_to :location
  
  #
  attr_accessor :country_name
  
  #Callbacks
  before_save :update_location
  
  def country
    if location
	  return location.country
	end
  end
  
  def day
    return full_date.day
  end
  
  def month
    return full_date.month
  end
  
  def year
    return full_date.year
  end
  
  def date_string
    return full_date
  end
  
  def date_string_with_month
    return full_date.with_month
  end
  
  #
  def update_location
    #If new location data is present, link the new location to the artist
	
    if !country_name.nil?
	  new_location = Location.country_equals(country_name).first
	  
	  if new_location.nil?
	    #didn't return anything, make one
		create_location(:country => country_name)
	  elsif new_location != self.location
	    #link it up
		self.location_id = new_location.id
	  end
	end
  end

end
